package com.example.cardgame
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import java.io.ByteArrayOutputStream

class SecondActivity : AppCompatActivity() {
    private lateinit var imageView: ImageView
    private lateinit var mName: EditText
    private lateinit var mXuehao: EditText
    private lateinit var mPhone: EditText
    private lateinit var mAddress: EditText

//
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    enableEdgeToEdge()
    setContentView(R.layout.activity_second)

    imageView = findViewById(R.id.imageView2)
    mName = findViewById(R.id.m_name)
    mXuehao = findViewById(R.id.m_xuehao)
    mPhone = findViewById(R.id.m_phone)
    mAddress = findViewById(R.id.m_address)

    val buttonTakePhoto = findViewById<Button>(R.id.buttonTakePhoto)
    buttonTakePhoto.setOnClickListener {
        takePhoto()
    }

    // 设置返回按钮点击事件
    val backButton = findViewById<Button>(R.id.backbutton)
    backButton.setOnClickListener {
        finish() // 直接结束活动，不返回数据
    }

    val radioGroup = findViewById<RadioGroup>(R.id.radioGroupGender)
    val checkboxTravel = findViewById<CheckBox>(R.id.checkboxTravel)
    val checkboxReading = findViewById<CheckBox>(R.id.checkboxReading)
    val checkboxShopping = findViewById<CheckBox>(R.id.checkboxShopping)
    val checkboxCodeing = findViewById<CheckBox>(R.id.checkboxCodeing)

    findViewById<Button>(R.id.submitButton).setOnClickListener {
        val selectedGenderId = radioGroup.checkedRadioButtonId
        val gender = if (selectedGenderId != -1) {
            findViewById<RadioButton>(selectedGenderId).text.toString()
        } else {
            "未选择"
        }

        val hobbies = mutableListOf<String>()
        if (checkboxTravel.isChecked) hobbies.add("旅游")
        if (checkboxReading.isChecked) hobbies.add("阅读")
        if (checkboxShopping.isChecked) hobbies.add("购物")
        if (checkboxCodeing.isChecked) hobbies.add("写代码")

        // 准备返回的数据
        val returnIntent = Intent()
        returnIntent.putExtra("name", mName.text.toString())
        returnIntent.putExtra("xuehao", mXuehao.text.toString())
        returnIntent.putExtra("phone", mPhone.text.toString())
        returnIntent.putExtra("address", mAddress.text.toString())
        returnIntent.putExtra("gender", gender)
        returnIntent.putExtra("hobbies", hobbies.joinToString(", "))

        // 将拍照的 Bitmap 转换为字节数组
        val imageBitmap = (imageView.drawable as BitmapDrawable).bitmap
        val stream = ByteArrayOutputStream()
        imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
        val byteArray = stream.toByteArray()
        returnIntent.putExtra("image", byteArray)

        setResult(RESULT_OK, returnIntent)
        finish() // 结束当前活动
    }
}
    private fun takePhoto() {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        cameraActivityResultLauncher.launch(intent)
    }

    private val cameraActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == RESULT_OK) {
            val data: Intent? = result.data
            val imageBitmap = data?.extras?.get("data") as? Bitmap
            imageView.setImageBitmap(imageBitmap) // 将拍照的图片显示在 ImageView 中
        }
    }
}
